﻿using Core.DataAccess.Model;
using Core.DataAccess.Model.iTool.Gateway;
using HandlerContextOptions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UserCenter.Interfaces;

namespace UserCenter.Implements
{
    public class UserBasics : IUserBasics
    {
        public async Task<UserDeleteResult> DeleteAsync(UserDeleteRequest requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                try
                {
                    var user = await context.SysUsers.FindAsync(requset.UserKey);
                    var userRole = await context.SysUserRoles.Where(item => item.UserRoleUserId == user.UserId).FirstAsync();
                    context.SysUsers.Remove(user);
                    context.SysUserRoles.Remove(userRole);
                    await context.SaveChangesAsync();

                    return new UserDeleteResult
                    {
                        IsSuccessful = true,
                        Description = "删除成功"
                    };
                }
                catch (Exception ex)
                {
                    return new UserDeleteResult
                    {
                        IsSuccessful = false,
                        Description = ex.Message
                    };
                }
            }
        }

        public async Task<UserLoginResult> LoginAsync(UserLoginRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                var user = await context.SysUsers
                   .Where(item => item.UserLoginName == requset.Name & item.UserPwd == requset.Pwd)
                   .FirstOrDefaultAsync();

                if (user == null)
                {
                    return new UserLoginResult
                    {
                        IsSuccessful = false,
                        Description = "用户名或密码错误,请检查"
                    };
                }
                else
                {
                    var roleid = await context.SysUserRoles
                    .Where(item => item.UserRoleUserId == user.UserId)
                    .Select(item => item.UserRoleId)
                    .FirstOrDefaultAsync();

                    var roleName = await context.SysRoles
                        .Where(item => item.RoleId == roleid)
                        .Select(item => item.RoleName)
                        .FirstOrDefaultAsync();

                    return new UserLoginResult
                    {
                        IsSuccessful = true,
                        Description = "登录成功",
                        UserKey = user.UserId,
                        UserName = user.UserName,
                        UserFace = string.Empty,
                        RoleKey = roleid,
                        RoleName = roleName,
                    };
                }
            }
        }

        public async Task<UserModifyResult> ModifyAsync(UserModifyRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                var user = await context.SysUsers
                    .Where(item => item.UserId == requset.UserId)
                    .FirstOrDefaultAsync();

                if (user == null)
                {
                    return new UserModifyResult
                    {
                        IsSuccessful = false,
                        Description = "用户不存在"
                    };
                }

                if (!string.IsNullOrWhiteSpace(requset.UserEmail))
                    user.UserEmail = requset.UserEmail;

                if (!string.IsNullOrWhiteSpace(requset.UserName))
                    user.UserName = requset.UserName;

                if (!string.IsNullOrWhiteSpace(requset.UserParameter))
                    user.UserParameter = requset.UserParameter;

                if (!string.IsNullOrWhiteSpace(requset.UserPwd))
                    user.UserPwd = requset.UserPwd;

                await context.SaveChangesAsync();

                if (requset.RoleKey > 0)
                {
                    var role = await context.SysUserRoles.Where(item => item.UserRoleUserId == user.UserId).FirstAsync();
                    role.UserRoleRoleId = requset.RoleKey;
                    await context.SaveChangesAsync();
                }

                return new UserModifyResult
                {
                    IsSuccessful = true,
                    Description = "修改成功"
                };
            }
        }

        public async Task<UserRegisterResult> RegisterAsync(UserRegisterRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                try
                {
                    await context.SysUsers.AddAsync(requset);
                    await context.SaveChangesAsync();

                    await context.SysUserRoles.AddAsync(new SysUserRole
                    {
                        UserRoleRoleId = requset.RoleKey,
                        UserRoleUserId = requset.UserId
                    });
                    await context.SaveChangesAsync();

                    return new UserRegisterResult
                    {
                        IsSuccessful = true,
                        Description = "注册成功"
                    };
                }
                catch (Exception ex)
                {
                    return new UserRegisterResult
                    {
                        IsSuccessful = false,
                        Description = ex.Message
                    };
                }
            }

        }
        public async Task<List<SysUser>> GetSysUsers(Pagination pagination)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                var result = context
                               .SysUsers
                               .Where(item => item.UserIsDelete != 1)
                               .PaginationAsync(pagination);
                return await result;
            }
        }
    }
}
